<?php

namespace App\Models\Factory\V0;

use App\Helpers\Http\HttpClient;
use App\Helpers\Utils;
use App\Models\AbsModelFactory;
use App\Models\Factory\CacheFactory;
use App\Models\Orm\SmsConfig;
use App\Models\Orm\SystemConfig;
use App\Strategies\SmsStrategy;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;

/**
 * 短信工厂
 * Class SmsFactory
 * @package App\Models\Factory
 */
class SmsFactory extends AbsModelFactory
{

    public function send($mobile)
    {
        $code = rand(1000, 9999);
        $msg = "您好，您的验证码是".$code.",请不要告诉他人!";

        if (preg_match('/^0?(13|14|15|17|18)[0-9]{9}$/', $mobile)) {

            $postArr = json_encode(array(
                'account' => self::DAHAN_ACCOUNT,
                'password' => self::DAHAN_PASSWORD,
                'msgid' => $msg,
                'phones' => $mobile,
                'content' => $msg,
                'sign' => '【卡卡现金】',
                'subcode' => '',
                'sendtime' => '',
            ));
            self::curlPost(self::API_SEND_URL, $postArr);
            SLogger::getStream()->info('这是大汉短信通道!');
            CacheFactory::putValueToCache($mobile . '_kk_code', $code, 5 * 60);
            CacheFactory::putValueToCache($mobile . '_send_kk_limit', $mobile, 60);

            return json_encode(array('isSuccess' => "1", 'status' => "1", "msg" => "短信验证码发送成功", "check" => $code));
        } else {
            return json_encode(array('isSuccess' => "0", 'status' => "0", "error" => "44000227", "msg" => "短信验证码发送失败"));
        }
    }


    /**
     * @param $mobile
     * 验证短信1分钟之内不能重复发送
     */
    public static function checkCodeExistenceTime($mobile)
    {

        $codeKey = $mobile . '_send_limit_kk';
        if (CacheFactory::existValueFromCache($codeKey)) {
            return false;
        }
        return true;
    }

    /**
     * 限制ip请求次数
     * 次数限制有效期为1天
     * @return mixed
     */
    public static function limitIp()
    {
        $ip = Utils::ipAddress();
        $key = 'kk_ip_value_' . $ip;
        if (Cache::has($key)) {
            Cache::increment($key, 1);
        } else {
            Cache::put($key, 1, Carbon::now()->second(60 * 60 * 24));
        }
//        dd(Cache::get($key));
        return Cache::get($key);
    }


    /**
     * 手机号次数限制
     * 次数限制有效期为1天
     * @param $mobile
     * @return mixed
     */
    public static function limitMobile($mobile)
    {
        $key = $mobile .'_send_kk_limit';
        if (Cache::has($key)) {
            Cache::increment($key, 1);
        } else {
            Cache::put($key, 1, Carbon::now()->second(60 * 60 * 24));
        }
        return Cache::get($key);

    }



    /**
     * 从cache中取出发送短信验证码时存进的随机数
     * @param $key
     * @return mixed
     */
    public static function getSmsRandomFromCache($key)
    {
        return Cache::get($key);
    }


    /**
     * 限制ip请求次数
     * 次数限制有效期为1天
     * @return mixed
     */
    public static function limitIpImage()
    {
        $ip = Utils::ipAddress();
        $key = 'kk_ip_image_' . $ip;
        if (Cache::has($key)) {
            Cache::increment($key, 1);
        } else {
            Cache::put($key, 1, Carbon::now()->second(60 * 60 * 24));
        }
        return Cache::get($key);
    }


    /**
     * 手机号次数限制
     * 次数限制有效期为1天
     * @param $mobile
     * @return mixed
     */
    public static function limitUserId($user_id)
    {
        $key = $user_id .'_image_kk_limit';
        if (Cache::has($key)) {
            Cache::increment($key, 1);
        } else {
            Cache::put($key, 1, Carbon::now()->second(60 * 60 * 24));
        }
        return Cache::get($key);

    }
}